package com.guaoran.micro.services.spring.cloud.client.controller;

import com.guaoran.micro.services.spring.cloud.client.service.feign.clients.SayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author : gucheng
 * @Description : <br/>
 *  客户端
 * @date :2018/12/2 13:00
 */
@RestController
public class ClientController {
    //获得本应用的名称
    @Value("${spring.application.name}")
    private String currentServiceName;
    @Value("${spring.cloud.server.application.name}")
    private String serverServiceName;
    //采用Ribbon方式实现负载均衡
    @Autowired
    @LoadBalanced
    private RestTemplate restTemplate;

    @Autowired
    private SayService sayService;


    /**
     * 采用Ribbon 负载均衡实现
     * @param message
     * @return
     */
    @GetMapping("/say")
    public String invokeSay(@RequestParam String message) {
        return restTemplate.getForObject("http://" + serverServiceName + "/say?message=" + message, String.class);
    }

    /**
     * feign 服务调用demo
     * @param message
     * @return
     */
    @GetMapping("/feign/say")
    public String feignSay(@RequestParam String message) {
        System.out.println("message======"+message+"====client");
        return sayService.say(message);
    }


}
